package cn.tedu.truetaste.controller;

import cn.tedu.truetaste.pojo.dto.SpuAddNewDTO;
import cn.tedu.truetaste.pojo.vo.SpuListItemVO;
import cn.tedu.truetaste.pojo.vo.SpuStandardVO;
import cn.tedu.truetaste.restful.JsonPage;
import cn.tedu.truetaste.service.IDishesService;
import cn.tedu.truetaste.service.ISpuService;
import cn.tedu.truetaste.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理SPU相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "前台菜品SPU管理模块")
@Slf4j
@RestController
@RequestMapping("dishes/spu")
@Validated
public class SpuController {

    @Autowired
    private ISpuService spuService;

    @Autowired
    private IDishesService dishesService;

    public SpuController() {
        log.debug("创建控制器类对象：SpuController");
    }

    // http://localhost:9899/spu/add-new
    @PostMapping("/add-new")
    @ApiOperation("新增SPU")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid SpuAddNewDTO spuAddNewDTO) {
        log.debug("开始处理【新增SPU】的请求，参数：{}", spuAddNewDTO);
        spuService.addNew(spuAddNewDTO);
        return JsonResult.ok();
    }

    @GetMapping("/list/{categoryId}")
    @ApiOperation("根据分类id分页查询spu列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类id",name ="categoryId",example = "3"),
            @ApiImplicitParam(value = "页码",name ="page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name ="pageSize",example = "2")
    })
    public JsonResult<JsonPage<SpuListItemVO>> listSpuByPage(
            @PathVariable Long categoryId, Integer page, Integer pageSize){
        JsonPage<SpuListItemVO> jsonPage =
                dishesService.listSpuByCategoryId(categoryId, page, pageSize);
        return JsonResult.ok(jsonPage);
    }
    @GetMapping("/{spuId}")
    @ApiOperation("根据spuId查询spu信息")
    @ApiImplicitParam(value = "spuId",name="spuId",example = "1")
    public JsonResult<SpuStandardVO> getFrontSpuById(@PathVariable Long spuId){
        SpuStandardVO spuStandardVO = dishesService.getSpuById(spuId);
        return JsonResult.ok(spuStandardVO);
    }

    @ApiOperation("根据spuId查询SpuDetail")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "spuId", value = "spuId",
            required = true, dataType = "long")
    @GetMapping("/list-by-spu")
    public JsonResult<List<SpuListItemVO>> listBySpuDetailId(Long spuDetailId) {
        log.debug("开始处理【根据spuId查询SpuDetail】的请求，参数：{}", spuDetailId);
        List<SpuListItemVO> list = spuService.listBySpuDetailId(spuDetailId);
        return JsonResult.ok(list);
    }

}